package schedule;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import common.Service;
import dicts.PaymentDict;
import entities.TSm;
import interfaces.IGlobalService;
import interfaces.ISmService;
import util.LogUtil;
import utils.DateUtil;

public class MortgageSchedule implements Job {

	private IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
	private ISmService smService = (ISmService) Service.create(ISmService.class);

	public void execute(JobExecutionContext context) throws JobExecutionException {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 保证金返还任务启动");
		
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" select sm.sm_no 'smNo' , sm.sm_amount 'smAmount' ");
			sb.append(" from t_sm sm ");
			sb.append(" where sm.sm_state='0' and sm.sm_type='" + PaymentDict.SM_MORTGAGE + "' and curdate()>sm.sm_date ");
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sb.toString());
			querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			List<LinkedHashMap<String, Object>> list = resultSet.getResultList();
			LogUtil.schedule("MORTGAGE", "今日保证金共 " + list.size() + " 笔");
			for (LinkedHashMap<String, Object> map : list) {
				TSm sm = new TSm();
				sm.setSmNo(map.get("smNo").toString());
				sm.setSmState("1");
				smService.persist(sm);
				LogUtil.schedule("MORTGAGE", map.get("smNo").toString() + "|" + map.get("smAmount").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		IGlobalService globalService = (IGlobalService) Service.create(IGlobalService.class);
		ISmService smService = (ISmService) Service.create(ISmService.class);

		System.out.println("mortgageSchedule");
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" select sm.sm_no 'smNo' , sm.sm_amount 'smAmount' ");
			sb.append(" from t_sm  sm");
			sb.append(" where sm.sm_state='0' and sm.sm_type='" + PaymentDict.SM_MORTGAGE + "' and curdate()>sm.sm_date ");
			QuerySet<Object> querySet = new QuerySet<Object>();
			querySet.setSqlInfo(sb.toString());
			querySet.setPageInfo(new PageInfo(1, Integer.MAX_VALUE));
			ResultSet<LinkedHashMap<String, Object>> resultSet = globalService.search(querySet);
			List<LinkedHashMap<String, Object>> list = resultSet.getResultList();
			LogUtil.schedule("MORTGAGE", "今日保证金共 " + list.size() + " 笔");
			for (LinkedHashMap<String, Object> map : list) {
				TSm sm = new TSm();
				sm.setSmNo(map.get("smNo").toString());
				sm.setSmState("1");
				smService.persist(sm);
				LogUtil.schedule("MORTGAGE", map.get("smNo").toString() + "|" + map.get("smAmount").toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
